<template>
  <el-form :model="loginModel" :rules="loginModel.rules" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">汽修宝商户商品管理平台</h3>
    <el-form-item prop="merchantCode">
      <el-input type="text" v-model="loginModel.merchantCode" auto-complete="off" placeholder="商户号" onkeydown="if(event.keyCode==13){window.myvue.doLogin()}"></el-input>
    </el-form-item>
        <el-form-item prop="loginName">
      <el-input type="text" v-model="loginModel.loginName" auto-complete="off" placeholder="账号" onkeydown="if(event.keyCode==13){window.myvue.doLogin()}"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginModel.password" auto-complete="off" placeholder="密码" onkeydown="if(event.keyCode==13){window.myvue.doLogin()}"></el-input>
    </el-form-item>


    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="doLogin">登录</el-button>
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>

    <!-- <el-form-item>
           <div class="flex-space">
  <el-button type="small" @click="doRegister">注册</el-button>
  <el-button type="small">忘记密码</el-button>
  </div>
    </el-form-item>  -->



  </el-form>
</template>

<script>
import * as Api from "@/api/Api.js";
import * as RoundApi from "@/api/RoundApi.js";
import * as Config from "@/api/Config.js";
export default {
  data() {
    return {
      logining: false,
      loginModel: {
        merchantCode: "",
        loginName: "",
        password: "",
        checked: true,
        rules: {
          merchantCode: [
            { required: true, message: "请输入商户号", trigger: "blur" }
            //{ validator: validaePass }
          ],
          loginName: [
            { required: true, message: "请输入手机号", trigger: "blur" }
            //{ validator: validaePass }
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" }
            //{ validator: validaePass2 }
          ]
        },

        doLogin: () => {
          this.$router.push("/test");
        }
      }
    };
  },
  watch: {
    "loginModel.loginName"(to, from) {
      // 对变化作出响应...
      console.log(to, from);
      if (this.loginModel.loginName.indexOf(" ") != -1) {
        this.$message("登录参数不存在特殊字符");
        this.loginModel.loginName = this.loginModel.loginName.replace(" ", "");
      }
    },
    "loginModel.merchantCode"(to, from) {
      // 对变化作出响应...
      console.log(to, from);
      if (this.loginModel.merchantCode.indexOf(" ") != -1) {
        this.$message("登录参数不存在特殊字符");

        this.loginModel.merchantCode = this.loginModel.merchantCode.replace(
          " ",
          ""
        );
      }
    }
  },
  methods: {
    doRegister() {
      
      this.$router.push("/test");
    },

    doLogin() {
      RoundApi.doLogin({
        merchantCode: this.loginModel.merchantCode,
        loginName: this.loginModel.loginName,
        password: this.loginModel.password
      }).then(res => {
        if (res.retCode) {
          let merchantRolePermissionLinkList = [];
          res.data.merchantRolePermissionLinkList.forEach((item, index) => {
            merchantRolePermissionLinkList.push(item.permissionId);
          });
          let local = window.localStorage;
          local.clear();
          local.setItem("getRoleTpye", res.data.roleType?res.data.roleType:null);
          local.setItem("merchantCode", this.loginModel.merchantCode);
          local.setItem("loginName", this.loginModel.loginName);
          console.log(merchantRolePermissionLinkList.join(","))
          local.setItem(
            "permissionIds",
            merchantRolePermissionLinkList.join(",")
          );



          let storage = window.sessionStorage;
          storage.clear();
          storage.setItem("token", res.data.token);
          storage.setItem("storeToken", res.data.merchant.token);
          storage.setItem("merchantId", res.data.merchant.merchantId);
          storage.setItem("merchantIcon", res.data.merchant.merchantIcon);
          storage.setItem("merchantName", res.data.merchant.merchantName);
          storage.setItem("liyangDatasource", res.data.merchant.liyangDatasource);

          this.$store.commit("SETROLETYPE",res.data.roleType?res.data.roleType:null);
          this.$store.commit("SETICON", res.data.merchant.merchantIcon);
          this.$store.commit("SETTOKEN", res.data.token);
          this.$store.commit("SETMERCHANTID", res.data.merchant.merchantId);
          this.$store.commit("SETMERCHANTNAME", res.data.merchant.merchantName);

          let model = {
            memberAudit: false, //注册审核
            basicMall: false, //商城
            seniorMall: false, //客户等级
            showModelRela: false, //商品适应车型
            componentSearch:false,
            maintenanceUpload:false,
            merchantUserId: res.data.merchantUserId
          };

          
    if(res.data.merchant.memberAudit){
console.log('注册审核通过')
    }




          document.title = res.data.merchant.merchantName + "商家管理后台";
          model.memberAudit = res.data.merchant.memberAudit;
          model.basicMall = res.data.merchant.basicMall;
          model.seniorMall = res.data.merchant.seniorMall;
          
          model.showModelRela = res.data.merchant.showModelRela;
     model.componentSearch = res.data.merchant.componentSearch;
model.maintenanceUpload = res.data.merchant.maintenanceUpload;

          storage.setItem("maintenanceUpload", model.maintenanceUpload);
          storage.setItem("componentSearch", model.componentSearch);
          storage.setItem("showModelRela", model.showModelRela);
          storage.setItem("memberAudit", model.memberAudit);
          storage.setItem("basicMall", model.basicMall);
          storage.setItem("seniorMall", model.seniorMall);
          storage.setItem("merchantUserId", model.merchantUserId);

          this.$store.commit("SETMODEL", model);
          this.$router.replace("/");
        } else {
          this.$message({
            type: "error",
            message: res.message
          });
        }
      });
    },
    handleReset2() {
      this.$refs.ruleForm2.resetFields();
      sessionStorage.setItem("user", JSON.stringify(user));
    }
  },
  mounted() {
    window.myvue = this;

    this.loginModel.merchantCode = localStorage.merchantCode;
    this.loginModel.loginName = localStorage.loginName;
  }
};
</script>

<style lang="scss" scoped>
.flex-space {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>